package hot_100;

public class 最长有效括号 {
    public static void main(String[] args) {

    }

    public int longestValidParentheses(String s) {
        int n = s.length();
        char[] ch = s.toCharArray();
        int[] dp = new int[n + 1];
        int ans = 0;
        for (int i = 1; i < n; i++) {
            int leng = i - dp[i - 1] - 1;
            if (leng >= 0 && ch[leng] == '(' && ch[i] == ')') {
                dp[i] = dp[i - 1] + 2 + dp[leng - 1];
                ans = Math.max(dp[i], ans);
            }
        }
        return ans;
    }
}
